﻿using Sys.Common;
using Sys.Core.Gen;
using Sys.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Sys.Api.Gen
{
    public class ContentApi : BaseApi
    {
        public string Grid(int rows, int page, string search)
        {
            var entities = ContentBll.I.SortEntities;

            if (!string.IsNullOrEmpty(search))
            {
                entities = entities.Where(m => m.Name.Contains(search));
            }

            var list = ContentBll.I.EntitiesPage(entities, rows, page);

            var select = list.Select(m => new
            {
                ID = m.ID,
                Name = m.Name,
                Tag = m.Tag,
                Remark = m.Remark,
                IsVisible = m.IsVisible,
                IntSort = m.IntSort,
                InsertTime = m.InsertTime,
                UpdateTime = m.UpdateTime
            });

            return GetGrid(entities.Count(), select.ToList());
        }

        public string Save(GenContent model)
        {
            JsonMsg result = new JsonMsg();

            if (string.IsNullOrEmpty(model.ID))
            {
                result.Message = "添加成功!";

                model.ID = StringHelper.GetGuid();

                model.IntSort = ContentBll.I.MaxSort(m => m.IntSort);
                model.InsertTime = DateTime.Now;
                model.UpdateTime = DateTime.Now;

                result.Success = ContentBll.I.Insert(model) > 0;
            }
            else
            {
                result.Message = "修改成功!";

                model.UpdateTime = DateTime.Now;

                result.Success = ContentBll.I.Update(model) > 0;
            }

            if (!result.Success) { result.Message = "操作失败!"; }

            return JsonHelper.ToJson(result);
        }

        public string Delete(string id)
        {
            JsonMsg result = new JsonMsg();

            result.Success = ContentBll.I.Delete(id) > 0;

            if (result.Success) { result.Message = "删除成功!"; }

            return JsonHelper.ToJson(result);
        }

        public string SetOrder(string id, string order)
        {
            JsonMsg result = new JsonMsg();

            var entities = ContentBll.I.Entities;

            bool success = false;

            result.Message = ContentBll.I.SetOrder(entities, id, "IntSort", order, out success);

            result.Success = success;

            return JsonHelper.ToJson(result);
        }

        public string Reset(string id, string tag)
        {
            JsonMsg result = new JsonMsg();

            GenContent model = ContentBll.I.GetByKey(id);

            if (model != null)
            {
                model.Tag = tag;

                result.Success = ContentBll.I.Update(model) > 0;

                if (result.Success) { result.Message = "操作成功!"; }
            }

            return JsonHelper.ToJson(result);
        }

        public string EnableOrDisable(string id, bool bit)
        {
            JsonMsg result = new JsonMsg();

            GenContent model = ContentBll.I.GetByKey(id);

            if (model != null)
            {
                if (model.IsVisible != bit)
                {
                    model.IsVisible = bit;

                    result.Success = ContentBll.I.Update(model) > 0;

                    if (result.Success)
                    {
                        result.Message = bit ? "启用成功!" : "禁用成功!";
                    }
                }
                else
                {
                    result.Message = bit ? "已经是启用状态!" : "已经是禁用状态!";
                }
            }

            return JsonHelper.ToJson(result);
        }

    }
}
